package com.atguigu.tingshu.account.receiver;

import com.alibaba.nacos.common.utils.StringUtils;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @author Wu Xingbin
 * @date 2023/11/17/20:51
 */
@Component
@Slf4j
public class AccountReceiver {

    @Autowired
    private UserAccountService userAccountService;


    @Autowired
    private RechargeInfoService rechargeInfoService;
    /**
     * 监听消息并添加到账号
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_USER_REGISTER)
    public void initUserAccount(ConsumerRecord<String,String> record){
        String userId = record.value();
        if (StringUtils.isBlank(userId)){
            return;
        }
        userAccountService.saveUserAccount(Long.valueOf(userId));
    }

    /**
     * 监听用户充值成功的消息
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS)
    public void rechargeSuccess(ConsumerRecord<String,String> record){
        String orderNo = record.value();
        if (StringUtils.isBlank(orderNo)){
            log.info("[账户服务]监听到充值成功消息，订单编号：{}", orderNo);
            rechargeInfoService.rechargeSuccess(orderNo);
        }
    }
}
